package com.xzqwjw.taskmanager.utility;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import android.os.Environment;
import android.os.StatFs;

public class SDCardHelper {

  public SDCardHelper() {
  }

  private String strSDPath = Environment.getExternalStorageDirectory().getPath() + "//";

  public String checkDBFile() {
    if (!IsExistSDCard()) {
      return "SD卡不存在！";
    }
    String strDBDirName = "PartyInfo";
    if (!HasDBDir(strDBDirName)) {
      return "数据库文件不存在！";
    }

    String strDBFile = getDBFile(strDBDirName);

    if (strDBFile.equals("") || strDBFile == null) {
      return "数据库文件不存在！";
    }

    return strDBFile;
  }

  public boolean IsExistSDCard() {
    return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
  }

  public boolean HasDBDir(String strDirName) {
    boolean boolFlag;
    String strDirPath = strSDPath + strDirName;
    File objFile = new File(strDirPath);
    if (objFile.exists()) {
      boolFlag = true;
    } else if (objFile.mkdir()) {
      boolFlag = true;
    } else {
      boolFlag = false;
    }
    return boolFlag;
  }

  public String getDBFile(String strDirName) {
    File objFile = new File(strSDPath + strDirName);
    File[] aryFiles = objFile.listFiles();
    if (aryFiles == null) {
      return "";
    }

    String strDBFile = "";
    String strFileName = "";
    List<String> listFileName = new ArrayList<>();
    for (int i = 0; i < aryFiles.length; i++) {
      objFile = aryFiles[i];
      strFileName = objFile.getName();
      if (getFileExtName(strFileName).equals("s3db")) {
        strDBFile = strSDPath + strDirName + "//" + strFileName;
        listFileName.add(strDBFile);
      }
    }

    if (listFileName.isEmpty() || listFileName == null || listFileName.size() > 1) {
      return "";
    } else {
      return listFileName.get(0);
    }
  }

  public long getSDFreeSize() {
    File objFile = Environment.getExternalStorageDirectory();
    StatFs objSF = new StatFs(objFile.getPath());
    long lngBlockSize = objSF.getBlockSizeLong();
    long lngFreeBlocks = objSF.getAvailableBlocksLong();
    // return freeBlocks * blockSize;
    // return (freeBlocks * blockSize)/1024;
    return (lngFreeBlocks * lngBlockSize) / 1024 / 1024;
  }

  public long getSDAllSize() {
    File objFile = Environment.getExternalStorageDirectory();
    StatFs objSF = new StatFs(objFile.getPath());
    long lngBlockSize = objSF.getBlockSizeLong();
    long lngAllBlocks = objSF.getAvailableBlocksLong();
    // return lngAllBlocks * lngBlockSize;
    // return (lngAllBlocks * lngBlockSize)/1024;
    return (lngAllBlocks * lngBlockSize) / 1024 / 1024;
  }

  // private String getFileName(String strFileFullName) {
  // if (strFileFullName.length() > 0 && strFileFullName != null) {
  // int i = strFileFullName.lastIndexOf(".");
  // if (i > -1 && i < strFileFullName.length()) {
  // return strFileFullName.substring(0, i);
  // }
  // }
  // return "";
  // }

  private String getFileExtName(String strFileFullName) {
    if (strFileFullName.length() > 0 && strFileFullName != null) {
      int i = strFileFullName.lastIndexOf(".");
      if (i > -1 && i < strFileFullName.length()) {
        return strFileFullName.substring(i + 1);
      }
    }
    return "";
  }

  public static byte[] readStream(InputStream objIS) throws Exception {
    byte[] aryBuffer = new byte[1024];
    int intLength = -1;
    ByteArrayOutputStream baosOS = new ByteArrayOutputStream();
    while ((intLength = objIS.read(aryBuffer)) != -1) {
      baosOS.write(aryBuffer, 0, intLength);
    }
    byte[] aryData = baosOS.toByteArray();
    baosOS.close();
    objIS.close();
    return aryData;
  }

}